﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JigLibX.Collision;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CColliderData))]
    public class CCollider: CComponent
    {
        #region fields

        private Dictionary<CollisionSkin ,CCollider> m_colliders;

        #endregion

        #region properties

        [ContentSerializerIgnore]
        public override CGameObject GameObject
        {
            get { return m_gameObject; }
            set
            {
                if (m_gameObject != null && m_gameObject.Node.Entity.Physic != null)
                    m_gameObject.Node.Entity.Physic.Skin.callbackFn -= OnCollision;
                base.GameObject = value;
            }
        }

        #endregion

        #region ctor

        public CCollider()
        {
            m_colliders = new Dictionary<CollisionSkin, CCollider>();
        }

        #endregion

        #region methods

        public override CComponent Clone(CGameObject gameObject)
        {
            CCollider col = new CCollider();
            col.GameObject = gameObject;
            
            return col;
        }

        public override void Initialize(CGameObject gameObject)
        {
            if (m_gameObject.Node.Entity.Physic != null)
                m_gameObject.Node.Entity.Physic.Skin.callbackFn += OnCollision;
        }

        public override void Update(CGameObject gameObject)
        {
            
            foreach (CollisionInfo info1 in m_gameObject.Node.Entity.Physic.Skin.Collisions)
            {
                if (!m_colliders.ContainsKey(info1.SkinInfo.Skin1))
                {
                    CNode n = DARE.CDare.Instance.SceneMgr.ActiveScene.ActiveRender3D.CollisionNode[info1.SkinInfo.Skin1];
                    CGameObject go = m_gameObject.Mgr.FindInstance(n);
                    if (go != null)
                    {
                        CCollider c = go.GetComponent<CCollider>();
                        if (c != null)
                            c.GameObject.OnCollisionExitMethod(this);
                        m_colliders.Remove(info1.SkinInfo.Skin1);
                    }
                }
            }
        }

        private bool OnCollision(CollisionSkin skin0, CollisionSkin skin1)
        {
            if (!m_colliders.ContainsKey(skin1))
            {
                CNode n = DARE.CDare.Instance.SceneMgr.ActiveScene.ActiveRender3D.CollisionNode[skin1];
                CGameObject go = m_gameObject.Mgr.FindInstance(n);
                if (go != null)
                {
                    CCollider c = go.GetComponent<CCollider>();
                    if (c != null)
                        c.GameObject.OnCollisionEnterMethod(this);
                    m_colliders.Add(skin1, c);
                }
            }
            return true;
        }

        #endregion
    }
}
